<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="&amp;#x524D;&amp;#x63D0; &amp;#x4ECA;&amp;#x5929;&amp;#x5728;&amp;#x7FA4;&amp;#x91CC;&amp;#x804A;&amp;#x5929;&amp;#x7684;&amp;#x65F6;&amp;#x5019;&amp;#x6709;&amp;#x7FA4;&amp;#x53CB;&amp;#x95EE;&amp;#x5982;&amp;#x4F55;&amp;#x6355;&amp;#x83B7;&amp;#x9519;&amp;#x8BEF;&amp;#x65E5;&amp;#x5FD7;&amp;#xFF0C">
<meta property="og:type" content="article">
<meta property="og:title" content="Android 捕获异常日志（上篇）">
<meta property="og:url" content="http://yoursite.com/2019/08/21/Android-捕获异常日志（上篇）/index.html">
<meta property="og:site_name" content="Silence潇湘夜雨">
<meta property="og:description" content="&amp;#x524D;&amp;#x63D0; &amp;#x4ECA;&amp;#x5929;&amp;#x5728;&amp;#x7FA4;&amp;#x91CC;&amp;#x804A;&amp;#x5929;&amp;#x7684;&amp;#x65F6;&amp;#x5019;&amp;#x6709;&amp;#x7FA4;&amp;#x53CB;&amp;#x95EE;&amp;#x5982;&amp;#x4F55;&amp;#x6355;&amp;#x83B7;&amp;#x9519;&amp;#x8BEF;&amp;#x65E5;&amp;#x5FD7;&amp;#xFF0C">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/1716569-2caa8755611fe702.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/1716569-1010920eff13ff10.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/1716569-3a47dcd9133561d4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2019-08-19T03:00:42.479Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android 捕获异常日志（上篇）">
<meta name="twitter:description" content="&amp;#x524D;&amp;#x63D0; &amp;#x4ECA;&amp;#x5929;&amp;#x5728;&amp;#x7FA4;&amp;#x91CC;&amp;#x804A;&amp;#x5929;&amp;#x7684;&amp;#x65F6;&amp;#x5019;&amp;#x6709;&amp;#x7FA4;&amp;#x53CB;&amp;#x95EE;&amp;#x5982;&amp;#x4F55;&amp;#x6355;&amp;#x83B7;&amp;#x9519;&amp;#x8BEF;&amp;#x65E5;&amp;#x5FD7;&amp;#xFF0C">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/1716569-2caa8755611fe702.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/08/21/Android-捕获异常日志（上篇）/">





  <title>Android 捕获异常日志（上篇） | Silence潇湘夜雨</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Silence潇湘夜雨</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">Android开发一枚</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home //首页"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user //关于"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags  //标签"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th //分类"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive //归档"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar //日程表"></i> <br>
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-sitemap">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap //站点地图"></i> <br>
            
            站点地图
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/08/21/Android-捕获异常日志（上篇）/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Silence潇湘夜雨">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Silence潇湘夜雨">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Android 捕获异常日志（上篇）</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-08-21T15:51:43+08:00">
                2019-08-21
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/工具类/" itemprop="url" rel="index">
                    <span itemprop="name">工具类</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.4k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  8
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="&#x524D;&#x63D0;"><a href="#&#x524D;&#x63D0;" class="headerlink" title="&#x524D;&#x63D0;"></a>&#x524D;&#x63D0;</h3><p> <strong>&#x4ECA;&#x5929;&#x5728;&#x7FA4;&#x91CC;&#x804A;&#x5929;&#x7684;&#x65F6;&#x5019;&#x6709;&#x7FA4;&#x53CB;&#x95EE;&#x5982;&#x4F55;&#x6355;&#x83B7;&#x9519;&#x8BEF;&#x65E5;&#x5FD7;&#xFF0C;&#x6211;&#x8BF4;&#x53EF;&#x4EE5;&#x81EA;&#x5DF1;&#x5199;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x7B2C;&#x4E09;&#x65B9;&#x7684;&#x6BD4;&#x5982;&#x817E;&#x8BAF;&#x7684;bugly&#xFF0C;&#x53CB;&#x76DF;&#x7684;&#x9519;&#x8BEF;&#x7EDF;&#x8BA1;&#x7B49;&#x7B49;&#xFF0C;&#x4F46;&#x662F;&#x90A3;&#x4E9B;&#x662F;&#x522B;&#x4EBA;&#x7684;&#x4E1C;&#x897F;&#xFF0C;&#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;&#x7A0B;&#x5E8F;&#x5458;&#x5F53;&#x7136;&#x662F;&#x8981;&#x77E5;&#x5176;&#x7136;&#xFF0C;&#x5E76;&#x4E14;&#x8981;&#x77E5;&#x5176;&#x6240;&#x4EE5;&#x7136;&#x3002;&#x56E0;&#x6B64;&#x4ECA;&#x5929;&#x5C31;&#x5728;&#x6B64;&#x5199;&#x4E00;&#x4E0B;&#x5173;&#x4E8E;&#x6355;&#x83B7;&#x9519;&#x8BEF;&#x65E5;&#x5FD7;&#x7684;&#x6587;&#x7AE0;&#xFF0C;&#x5E0C;&#x671B;&#x53EF;&#x4EE5;&#x7ED9;&#x65B0;&#x624B;&#x6307;&#x5BFC;&#xFF0C;&#x5927;&#x4F6C;&#x8BF7;&#x7ED5;&#x884C;&#x3002;</strong></p>
<h3 id="&#x9996;&#x5148;"><a href="#&#x9996;&#x5148;" class="headerlink" title="&#x9996;&#x5148;"></a>&#x9996;&#x5148;</h3><p>  &#x8981;&#x6355;&#x83B7;&#x9519;&#x8BEF;&#x65E5;&#x5FD7;&#x5F53;&#x7136;&#x662F;&#x8C03;&#x7528;&#x7CFB;&#x7EDF;&#x7684;&#x4E86;&#xFF0C;&#x8FD9;&#x6837;&#x6700;&#x65B9;&#x4FBF;&#xFF0C;&#x4E5F;&#x662F;&#x5927;&#x5BB6;&#x5E38;&#x7528;&#x7684;&#x4E86;&#xFF0C;&#x5E9F;&#x8BDD;&#x4E0D;&#x591A;&#x8BF4;&#xFF0C;&#x76F4;&#x63A5;&#x4E0A;&#x56FE;&#xFF0C;no pic say a xx.<br><img src="https://upload-images.jianshu.io/upload_images/1716569-2caa8755611fe702.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="&#x9519;&#x8BEF;&#x65E5;&#x5FD7;.png"></p>
<h3 id="&#x5176;&#x6B21;"><a href="#&#x5176;&#x6B21;" class="headerlink" title="&#x5176;&#x6B21;"></a>&#x5176;&#x6B21;</h3><p> <strong>&#x4E0A;&#x9762;&#x7684;&#x56FE;&#x662F;&#x65E5;&#x5FD7;&#x4FE1;&#x606F;&#xFF0C;&#x4E0B;&#x9762;&#x6765;&#x770B;&#x770B;&#x4EE3;&#x7801;&#x5982;&#x4F55;&#x7F16;&#x5199;&#x3002;</strong></p>
<ul>
<li><p>&#x6355;&#x83B7;&#x9519;&#x8BEF;&#x65E5;&#x5FD7;&#x4FE1;&#x606F;&#x7C7B;</p>
<pre><code> public class CrashHandler implements UncaughtExceptionHandler {

 private static final String TAG = &quot;CrashHandler&quot;;
 private static final boolean DEBUG = true;

 private static final String FILE_NAME = &quot;crash&quot;;

 // log&#x6587;&#x4EF6;&#x7684;&#x540E;&#x7F00;&#x540D;
 private static final String FILE_NAME_SUFFIX = &quot;.txt&quot;;

 private static CrashHandler sInstance = new CrashHandler();

 // &#x7CFB;&#x7EDF;&#x9ED8;&#x8BA4;&#x7684;&#x5F02;&#x5E38;&#x5904;&#x7406;&#xFF08;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x7CFB;&#x7EDF;&#x4F1A;&#x7EC8;&#x6B62;&#x5F53;&#x524D;&#x7684;&#x5F02;&#x5E38;&#x7A0B;&#x5E8F;&#xFF09;
 private UncaughtExceptionHandler mDefaultCrashHandler;

 private Context mContext;
 //log&#x8DEF;&#x5F84;
 private  String mLogPath=null;

 // &#x6784;&#x9020;&#x65B9;&#x6CD5;&#x79C1;&#x6709;&#xFF0C;&#x9632;&#x6B62;&#x5916;&#x90E8;&#x6784;&#x9020;&#x591A;&#x4E2A;&#x5B9E;&#x4F8B;&#xFF0C;&#x5373;&#x91C7;&#x7528;&#x5355;&#x4F8B;&#x6A21;&#x5F0F;
private CrashHandler() {
}

 public static CrashHandler getInstance() {
     return sInstance;
}

// &#x8FD9;&#x91CC;&#x4E3B;&#x8981;&#x5B8C;&#x6210;&#x521D;&#x59CB;&#x5316;&#x5DE5;&#x4F5C;
public void init(Context context,String logPath) {</code></pre><p>   &#x200B;    // &#x83B7;&#x53D6;&#x7CFB;&#x7EDF;&#x9ED8;&#x8BA4;&#x7684;&#x5F02;&#x5E38;&#x5904;&#x7406;&#x5668;</p>
<pre><code>mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler();
// &#x5C06;&#x5F53;&#x524D;&#x5B9E;&#x4F8B;&#x8BBE;&#x4E3A;&#x7CFB;&#x7EDF;&#x9ED8;&#x8BA4;&#x7684;&#x5F02;&#x5E38;&#x5904;&#x7406;&#x5668;
Thread.setDefaultUncaughtExceptionHandler(this);
// &#x83B7;&#x53D6;Context&#xFF0C;&#x65B9;&#x4FBF;&#x5185;&#x90E8;&#x4F7F;&#x7528;
mContext = context.getApplicationContext();
this.mLogPath=logPath;
}

/**</code></pre><p>   &#x200B;    * &#x8FD9;&#x4E2A;&#x662F;&#x6700;&#x5173;&#x952E;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x5F53;&#x7A0B;&#x5E8F;&#x4E2D;&#x6709;&#x672A;&#x88AB;&#x6355;&#x83B7;&#x7684;&#x5F02;&#x5E38;&#xFF0C;&#x7CFB;&#x7EDF;&#x5C06;&#x4F1A;&#x81EA;&#x52A8;&#x8C03;&#x7528;#uncaughtException&#x65B9;&#x6CD5;<br>   &#x200B;    * thread&#x4E3A;&#x51FA;&#x73B0;&#x672A;&#x6355;&#x83B7;&#x5F02;&#x5E38;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;ex&#x4E3A;&#x672A;&#x6355;&#x83B7;&#x7684;&#x5F02;&#x5E38;&#xFF0C;&#x6709;&#x4E86;&#x8FD9;&#x4E2A;ex&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x5F97;&#x5230;&#x5F02;&#x5E38;&#x4FE1;&#x606F;&#x3002;<br>   &#x200B;    */<br>   &#x200B;    @Override<br>   &#x200B;    public void uncaughtException(Thread thread, Throwable ex) {<br>   &#x200B;    try {<br>   &#x200B;    // &#x5BFC;&#x51FA;&#x5F02;&#x5E38;&#x4FE1;&#x606F;&#x5230;SD&#x5361;&#x4E2D;<br>   &#x200B;         dumpExceptionToSDCard(ex);<br>   &#x200B;    // &#x8FD9;&#x91CC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x7F51;&#x7EDC;&#x4E0A;&#x4F20;&#x5F02;&#x5E38;&#x4FE1;&#x606F;&#x5230;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x4FBF;&#x4E8E;&#x5F00;&#x53D1;&#x4EBA;&#x5458;&#x5206;&#x6790;&#x65E5;&#x5FD7;&#x4ECE;&#x800C;&#x89E3;&#x51B3;bug<br>   &#x200B;    uploadExceptionToServer();<br>   &#x200B;    } catch (IOException e) {<br>   &#x200B;    e.printStackTrace();<br>   &#x200B;    }</p>
<pre><code>     // &#x6253;&#x5370;&#x51FA;&#x5F53;&#x524D;&#x8C03;&#x7528;&#x6808;&#x4FE1;&#x606F;
   ex.printStackTrace();

 // &#x5982;&#x679C;&#x7CFB;&#x7EDF;&#x63D0;&#x4F9B;&#x4E86;&#x9ED8;&#x8BA4;&#x7684;&#x5F02;&#x5E38;&#x5904;&#x7406;&#x5668;&#xFF0C;&#x5219;&#x4EA4;&#x7ED9;&#x7CFB;&#x7EDF;&#x53BB;&#x7ED3;&#x675F;&#x6211;&#x4EEC;&#x7684;&#x7A0B;&#x5E8F;&#xFF0C;&#x5426;&#x5219;&#x5C31;&#x7531;&#x6211;&#x4EEC;&#x81EA;&#x5DF1;&#x7ED3;&#x675F;&#x81EA;&#x5DF1;
 if (mDefaultCrashHandler != null) {
     mDefaultCrashHandler.uncaughtException(thread, ex);
 } else {
     Process.killProcess(Process.myPid());
 }

}

/**</code></pre><p> &#x200B;    * &#x5199;&#x5165;SD&#x5361;<br> &#x200B;   *<br> &#x200B;   * @param ex<br> &#x200B;   * @throws IOException<br> &#x200B;   */<br>&#x200B;      @SuppressLint(&#x201C;SimpleDateFormat&#x201D;)<br>&#x200B;      private void dumpExceptionToSDCard(Throwable ex) throws IOException {<br> &#x200B;    // &#x5982;&#x679C;SD&#x5361;&#x4E0D;&#x5B58;&#x5728;&#x6216;&#x65E0;&#x6CD5;&#x4F7F;&#x7528;&#xFF0C;&#x5219;&#x65E0;&#x6CD5;&#x628A;&#x5F02;&#x5E38;&#x4FE1;&#x606F;&#x5199;&#x5165;SD&#x5361;<br> &#x200B;    if (!Environment.getExternalStorageState().equals(<br> &#x200B;            Environment.MEDIA_MOUNTED)) {<br> &#x200B;        if (DEBUG) {<br> &#x200B;            Log.e(TAG, &#x201C;sdcard unmounted,skip dump exception&#x201D;);<br> &#x200B;            return;<br> &#x200B;        }<br> &#x200B;    }</p>
<pre><code>File dir = new File(mLogPath);
if (!dir.exists()) {
    dir.mkdirs();
}
long current = System.currentTimeMillis();
String time = new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss&quot;)
        .format(new Date(current));
// &#x4EE5;&#x5F53;&#x524D;&#x65F6;&#x95F4;&#x521B;&#x5EFA;log&#x6587;&#x4EF6;
File file = new File(mLogPath + FILE_NAME + time
        + FILE_NAME_SUFFIX);

try {
    PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(
            file)));
    // &#x5BFC;&#x51FA;&#x53D1;&#x751F;&#x5F02;&#x5E38;&#x7684;&#x65F6;&#x95F4;
    pw.println(time);

    // &#x5BFC;&#x51FA;&#x624B;&#x673A;&#x4FE1;&#x606F;
    dumpPhoneInfo(pw);

    pw.println();
    // &#x5BFC;&#x51FA;&#x5F02;&#x5E38;&#x7684;&#x8C03;&#x7528;&#x6808;&#x4FE1;&#x606F;
    ex.printStackTrace(pw);

    pw.close();
} catch (Exception e) {
    Log.e(TAG, &quot;dump crash info failed&quot;);
}</code></pre><p>&#x200B;      }</p>
<pre><code>private void dumpPhoneInfo(PrintWriter pw) throws NameNotFoundException {</code></pre><p> &#x200B;    // &#x5E94;&#x7528;&#x7684;&#x7248;&#x672C;&#x540D;&#x79F0;&#x548C;&#x7248;&#x672C;&#x53F7;<br> &#x200B;    PackageManager pm = mContext.getPackageManager();<br> &#x200B;    PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(),<br> &#x200B;            PackageManager.GET_ACTIVITIES);<br> &#x200B;    pw.print(&#x201C;App Version: &#x201C;);<br> &#x200B;    pw.print(pi.versionName);<br> &#x200B;    pw.print(&#x2018;_&#x2019;);<br> &#x200B;    pw.println(pi.versionCode);</p>
<pre><code>// android&#x7248;&#x672C;&#x53F7;
pw.print(&quot;OS Version: &quot;);
pw.print(Build.VERSION.RELEASE);
pw.print(&quot;_&quot;);
pw.println(Build.VERSION.SDK_INT);

// &#x624B;&#x673A;&#x5236;&#x9020;&#x5546;
pw.print(&quot;Vendor: &quot;);
pw.println(Build.MANUFACTURER);

// &#x624B;&#x673A;&#x578B;&#x53F7;
pw.print(&quot;Model: &quot;);
pw.println(Build.MODEL);

// cpu&#x67B6;&#x6784;
pw.print(&quot;CPU ABI: &quot;);
pw.println(Build.CPU_ABI);</code></pre><p>&#x200B;      }</p>
<pre><code>/**</code></pre><p> &#x200B;    * &#x4E0A;&#x4F20;&#x5230;&#x670D;&#x52A1;&#x5668;(&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x5B9E;&#x73B0;)<br> &#x200B;    */<br>&#x200B;      private void uploadExceptionToServer() {<br>&#x200B;      }</p>
<pre><code>}</code></pre></li>
<li><p>APP&#xFF08;&#x81EA;&#x5B9A;&#x4E49;&#x7684;Application&#xFF09;</p>
<pre><code>public class APP extends Application {
 //log&#x8DEF;&#x5F84;
 private static final String LOG_PATH= Environment
      .getExternalStorageDirectory().getPath() + File.separator + &quot;Live&quot; + File.separator
      + &quot;log&quot; + File.separator;

 @Override
public void onCreate() {
  super.onCreate();
  CrashHandler.getInstance().init(this,LOG_PATH);
}
}</code></pre></li>
<li><p>MainActivity</p>
<pre><code>class MainActivity : AppCompatActivity(){</code></pre></li>
</ul>
<pre><code>var mBtnSecond:Button?=null;

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  setContentView(R.layout.activity_main)
  checkPermission()
  initView()
}


fun  initView(){
  mBtnSecond=findViewById(R.id.btn_second)
  mBtnSecond?.setOnClickListener{
     var intent= Intent(this,SecondActivity::class.java)
     startActivity(intent)
 }
}


/**
 * 6.0&#x4EE5;&#x4E0B;&#x7248;&#x672C;(&#x7CFB;&#x7EDF;&#x81EA;&#x52A8;&#x7533;&#x8BF7;) &#x4E0D;&#x4F1A;&#x5F39;&#x6846;
 * &#x6709;&#x4E9B;&#x5382;&#x5546;&#x4FEE;&#x6539;&#x4E86;6.0&#x7CFB;&#x7EDF;&#x7533;&#x8BF7;&#x673A;&#x5236;&#xFF0C;&#x4ED6;&#x4EEC;&#x4FEE;&#x6539;&#x6210;&#x7CFB;&#x7EDF;&#x81EA;&#x52A8;&#x7533;&#x8BF7;&#x6743;&#x9650;&#x4E86;
 */
private fun checkPermission(){
  val permissionItems = ArrayList&lt;PermissionItem&gt;()
  permissionItems.add(PermissionItem(Manifest.permission.READ_EXTERNAL_STORAGE, &quot;&#x8BFB;&#x53D6;&#x7A7A;&#x95F4;&quot;, R.drawable.permission_ic_phone))
  permissionItems.add(PermissionItem(Manifest.permission.WRITE_EXTERNAL_STORAGE,&quot;&#x5B58;&#x50A8;&#x7A7A;&#x95F4;&quot;,R.drawable.permission_ic_storage))
  HiPermission.create(this)
          .title(&quot;&#x4EB2;&#x7231;&#x7684;&#x4E0A;&#x5E1D;&quot;)
          .msg(&quot;&#x4E3A;&#x4E86;&#x80FD;&#x591F;&#x6B63;&#x5E38;&#x4F7F;&#x7528;&#xFF0C;&#x8BF7;&#x5F00;&#x542F;&#x8FD9;&#x4E9B;&#x6743;&#x9650;&#x5427;&#xFF01;&quot;)
          .permissions(permissionItems)
          .style(R.style.PermissionDefaultBlueStyle)
          .animStyle(R.style.PermissionAnimScale)
          .checkMutiPermission(object : PermissionCallback {
              override fun onClose() {
                  Toast.makeText(this@MainActivity,&quot;&#x7528;&#x6237;&#x5173;&#x95ED;&#x4E86;&#x6743;&#x9650;&quot;,Toast.LENGTH_LONG).show();
              }

              override fun onFinish() {
                  Toast.makeText(this@MainActivity,&quot;&#x521D;&#x59CB;&#x5316;&#x5B8C;&#x6BD5;&#xFF01;&quot;,Toast.LENGTH_LONG).show();
              }

              override fun onDeny(permission: String, position: Int) {
              }

              override fun onGuarantee(permission: String, position: Int) {
              }
          })
}

 }</code></pre><p><img src="https://upload-images.jianshu.io/upload_images/1716569-1010920eff13ff10.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="MainActivity.png"></p>
<ul>
<li><p>CrashActivity</p>
<pre><code>public class CrashActivity extends AppCompatActivity {

Button mBtnCrash;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_second);
 mBtnCrash=findViewById(R.id.btn_crash);
 mBtnCrash.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         Toast.makeText(CrashActivity.this,&quot;&#x51FA;&#x73B0;&#x5F02;&#x5E38;&#x4E86;&quot;,Toast.LENGTH_LONG).show();
             throw new RuntimeException(toUtf8(&quot;&#x51FA;&#x73B0;&#x5F02;&#x5E38;&#x4E86;&quot;));
     }
 });
}

public static String toUtf8(String str) {
  String result = null;
  try {
      result = new String(str.getBytes(&quot;UTF-8&quot;), &quot;UTF-8&quot;);
  } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
  }
  return result;
}
}</code></pre></li>
</ul>
<p><img src="https://upload-images.jianshu.io/upload_images/1716569-3a47dcd9133561d4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="CrashActivity.png"></p>
<h3 id="&#x6700;&#x540E;"><a href="#&#x6700;&#x540E;" class="headerlink" title="&#x6700;&#x540E;"></a>&#x6700;&#x540E;</h3><p> &#x8FD9;&#x91CC;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;&#x5728;MainActivity&#x4E2D;&#x7528;&#x7684;&#x662F;Kotlin&#x5199;&#x7684;&#x6743;&#x9650;&#x63A7;&#x5236;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8FD0;&#x884C;&#x65F6;&#x6743;&#x9650;<br> <a href="https://github.com/yewei02538/HiPermission" target="_blank" rel="noopener">implementation &#x2018;me.weyye.hipermission:library:1.0.7&#x2019;</a><br>&#x8981;&#x4FDD;&#x5B58;&#x65E5;&#x5FD7;&#x5F53;&#x7136;&#x9700;&#x8981;SD&#x5361;&#x7684;&#x8BFB;&#x5199;&#x6743;&#x9650;&#x3002;<br><a href="https://gitee.com/1032200695/CrashException" target="_blank" rel="noopener">&#x9879;&#x76EE;&#x5730;&#x5740;</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/08/21/Android-TextView-点击超链接跳转实现/" rel="next" title="Android TextView 点击超链接跳转实现">
                <i class="fa fa-chevron-left"></i> Android TextView 点击超链接跳转实现
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/08/21/Android-捕获异常日志（下篇）/" rel="prev" title="Android 捕获异常日志（下篇）">
                Android 捕获异常日志（下篇） <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Silence潇湘夜雨</p>
              <p class="site-description motion-element" itemprop="description">Android开发一枚</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">55</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">16</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/muyishuangfeng" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/1e2eec6f972c" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>简书</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://juejin.im/user/58df0abf61ff4b006b115e43" target="_blank" title="掘金">
                      
                        <i class="fa fa-fw fa-star"></i>掘金</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://twitter.com/yangke20792452" target="_blank" title="Twitter">
                      
                        <i class="fa fa-fw fa-twitter"></i>Twitter</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#前提"><span class="nav-number">1.</span> <span class="nav-text">前提</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#首先"><span class="nav-number">2.</span> <span class="nav-text">首先</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#其次"><span class="nav-number">3.</span> <span class="nav-text">其次</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#最后"><span class="nav-number">4.</span> <span class="nav-text">最后</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Silence潇湘夜雨</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">114.8k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

  <!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/love.js"></script>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"model":{"scale":1,"hHeadPos":0.5,"vHeadPos":0.618},"display":{"superSample":2,"width":150,"height":300,"position":"right","hOffset":0,"vOffset":-20},"mobile":{"show":false},"react":{"opacityDefault":0.5,"opacityOnHover":0.5},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
